function checkValidateType(ValidateType, value){
    var length = '';
    if(ValidateType.indexOf('(') != '-1'){
        length = ValidateType.substr(ValidateType.indexOf('(')+1,ValidateType.indexOf(')') - ValidateType.indexOf('(')-1);
        ValidateType = ValidateType.substr(0,ValidateType.indexOf('('));
    }
    var Expression;
    switch(ValidateType){
        case 'email':
            Expression = /^([\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4})*$/;
            break;

        case 'email_obligatory':
            Expression = /^([\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4})+$/;
            break;

        case 'decimal':
            Expression = /^[0-9]+\,?[0-9]+$|^[0-9]+\.?[0-9]+$/;
            break;
        case 'decimal_empty':
            Expression = /^[0-9]*\,?[0-9]*$|^[0-9]*\.?[0-9]*$/;
            break;

        case 'Y-d-m':
            Expression = /^[2]{1}[0-9]{3}\-[0-1]?[0-9]{1}\-[0-3]?[0-9]{1}$/;
            break;

        case 'd-m-Y':
            Expression = /^[0-3]?[0-9]{1}\-[0-1]?[0-9]{1}\-[1-2]{1}[0-9]{3}$/;
            break;

        case 'Y':
            Expression = /^[2]{1}[0-9]{3}$/;
            break;

        case 'm':
            Expression = /^0?[0-9]{1}$|^1?[0-2]{1}$/;
            break;

        case 'd':
            Expression = /^[0-3]?[0-9]{1}$/;
            break;

        case 'text':
            Expression = /^[a-zA-ZЂ-џ\s]*$/;
            break;

        case 'text_obligatory':
            Expression = /^[^<]+$/;
            break;


        case 'naslov':
            Expression = /^[a-zA-ZЂ-џ0-9\s\,\.\-\_]+$/;
            break;

        case 'text2_obligatory':
            Expression = /^[\a-zA-Z0-9\-\_\.\,\s]+$/;
            break;

        case 'text3_obligatory':
            Expression = /^[\a-zA-ZЂ-џ0-9\;\:\-\_\,\.\\\|\$\@\+\(\)\&\%\s]+$/;
            break;
            
        case 'name':
            Expression = /^[a-zA-ZÀ-əΑ-ө\s-\,]{1,}$/;
            break;

        case 'name+empty':
            Expression = /^[a-zA-ZÀ-əΑ-ө\s\,]*$/;
            break;


        case 'number':
            if(length != ''){
                Expression = new RegExp("^[0-9]{"+length+"}$|^[0-9]{0}$");
            }else{
                Expression = /^[0-9]*$/;
            }
            break;

        case 'number_obligatory':
            if(length != ''){
                Expression = new RegExp("^[0-9]{"+length+"}$");
            }else{
                Expression = /^[0-9]+$/;
            }
            break;

        case 'alphanumeric':
            Expression = /^[a-z0-9A-Z\s]+$/;
            break;

        case 'phone':
            Expression = /^\+?[0-9\s-]{6,17}$/;
            break;

        case 'passwordSpecial':
            Expression = /^([a-z0-9A-ZЂ-џ*_\#\%\s\'\;\]\[\}\{\"\:\`\~\!\@\^]{4,})*$/;
            break;

        default:
            alert('Uknown type of validator type');
            break;
    }
    //    if(ValidateType =='apenaf'){
    //      alert(value);
    //      alert(value.match(Expression));
    //    }
    //
    var isValid = value.match(Expression);
    return isValid;
}

function checkForm(formObj){
    var error = true;
    //alert($('input[name=username]').css('display'));
    removeErrorNote($('p.errorNote',$('div.jsErrorMessage')));
    var inputTextElems = $('input',formObj);
    if($(inputTextElems).length > 0){
        for(var i=0; i<$(inputTextElems).length; i++){
            var inputType = $(inputTextElems).eq(i).attr("type");
            var validateType;
            var errorNote;
            if((inputType !='hidden') && ($(inputTextElems).eq(i).css('display') !='none') &&
                !($(inputTextElems).eq(i).attr("disabled")) &&
                (inputType=="text" || inputType=="password") &&
                $(inputTextElems).eq(i).attr("valtype")){
                if($(inputTextElems).eq(i).attr("obligatory") == 'false' && $(inputTextElems).eq(i).val() == ''){
                    continue;
                }
                validateType = $(inputTextElems).eq(i).attr("valtype");
                errorNote = $(inputTextElems).eq(i).attr("errorNote");
                //removeErrorNote($(inputTextElems).eq(i));
                if(!checkValidateType(validateType, $(inputTextElems).eq(i).val())){
                    displayErrorNote(formObj,$(inputTextElems).eq(i), errorNote);
                    //$(inputTextElems).eq(i).focus();
                    //alert(errorNote);
                    error = false;
                }
            }
        }
        var textPasswords = $('input[type=password][valtype]',formObj);
        if($(textPasswords).length > 0){
            for(var k=0; k<$(textPasswords).length/2; k++){
                var pass_value = $(textPasswords).eq(k).val();
                var field_name = $(textPasswords).eq(k).attr('name');
                var confirm_field = $('input[name='+field_name+'_confirm]',formObj);
                errorNote = $(textPasswords).eq(k).attr("errorNote");
                //removeErrorNote($(textPasswords).eq(k));
                if(pass_value != $(confirm_field).val()){
                    //$(textPasswords).eq(k).focus();
                    displayErrorNote(formObj,$(textPasswords).eq(k), errorNote);
                    error = false;
                }
            }
        }
    }

    var textAreaElems = $('textarea',formObj);
    if($(textAreaElems).length > 0){
        for(var j=0; j<$(textAreaElems).length; j++){
            if(($(textAreaElems).eq(j).attr("valtype")) && !($(textAreaElems).eq(i).attr("disabled"))){
                if($(textAreaElems).eq(j).attr("obligatory") == 'false' && $(textAreaElems).eq(j).val() == ''){
                    continue;
                }
                var textType = $(textAreaElems).eq(j).attr("valtype");
                var textErrorNote =$(textAreaElems).eq(j).attr("errorNote");
                //removeErrorNote($(textAreaElems).eq(j));
                if(!checkValidateType(textType, $(textAreaElems).eq(j).val())){
                    displayErrorNote(formObj,$(textAreaElems).eq(j), textErrorNote);
                    //$(textAreaElems).eq(j).focus();
                    //alert(textErrorNote);
                    error = false;
                }
            }
        }
    }
    var selectElems = $('select[valtype=obligatory]',formObj);
    if($(selectElems).length > 0){
        for(var s=0; s<$(selectElems).length; s++){
            if(!($(selectElems).eq(s).attr("disabled")) && ($(selectElems).eq(s).css('display') !='none')){
                if($(selectElems).eq(s).attr("obligatory") == 'false' && $(selectElems).eq(s).val() == ''){
                    continue;
                }
                var selErrorNote =$(selectElems).eq(s).attr("errorNote");
                //removeErrorNote($(selectElems).eq(s));
                if($(selectElems).eq(s).val() == ''){
                    displayErrorNote(formObj,$(selectElems).eq(s), selErrorNote);
                    //$(selectElems).eq(s).focus();
                    //alert(selErrorNote);
                    error = false;
                }
            }
        }
    }
    if(!error){
        //window.location.hash = "jsErrorMessage";
        $.scrollTo('a[name=jsErrorMessage]','slow');
    }
    return error;
}

function submitForm(formName){
    var formObj = $('form[name='+formName+']');
    if (!checkForm(formObj)){
        return false;
    }
    $(formObj).submit();
    return true;
}

function displayErrorNote(formObj,obj, note){
    //    if($(formObj).attr('alt') == 'tab_form'){
    //      var tabContainerDiv = $('div',formObj);
    //      var parrentDiv = $(obj).parents('div.tab');
    //      var liElems = $('ul.tab_nav',tabContainerDiv);
    //      //alert($(liElems).eq(1).attr('rel'));
    //      var liElem = $("li[rel='#"+$(parrentDiv).attr('id')+"']",liElems);
    //
    //      selectTab($(tabContainerDiv).attr('id'),liElem);
    //    }
    var newErrorNoteDiv = document.createElement("p");
    $(newErrorNoteDiv).attr("class", "errorNote");
    var newErrorNoteText = document.createTextNode(note);
    $(newErrorNoteDiv).append(newErrorNoteText);
    $(obj).css({
        "border":"1px solid #CC0001",
        "backgroundColor":"#FFE5E5",
        "backgroundImage":"none"
    });
    $(obj).addClass('hasError');
    //    var pos = $(obj).offset();
    //    $(newErrorNoteDiv).css({
    //              position:'absolute',
    //              backgroundColor:'#fff',
    //              border:'1px solid #ccc',
    //              cursor:'pointer',
    //              padding:'2px',
    //              color:'red',
    //              top:pos.top + 22,
    //              left:pos.left + 2
    //            });
    //    $(newErrorNoteDiv).click(function(){
    //        $(this).remove();
    //    });
    if($('p.errorNote').length == 0){
        $(newErrorNoteDiv).insertAfter($('img',$('div.jsErrorMessage')));
    }else{
        $(newErrorNoteDiv).insertAfter($('p.errorNote:eq('+($('.errorNote').length -1)+')',$('div.errorMessage')));
    }
    $('div.jsErrorMessage').css({
        'display':'block'
    });
    
}

function removeErrorNote(obj){
    if($(obj).length > 0){
        $(obj).remove();
    }
    $(obj).css({
        "border":"1px solid #CC0001",
        "backgroundColor":"#FFE5E5",
        "backgroundImage":"none"
    });
    $('.hasError').removeAttr('style');
    $('.hasError').removeClass('hasError');

}


//function submitForm(form){
//        if (!checkForm(form)){
//          return false;
//        }
//      form.submit();
//      return true;
//}

function checkDecimalFormat(elem){
    removeErrorNote(elem);

    var value = elem.value;
    valid = false;
    if(value==""){
        return true;
    }

    valid = value.match(/^[0-9]*\,?[0-9]*$|^[0-9]*\.?[0-9]*$/);
    if(valid){
        var str= (value.indexOf('.')==-1)?value.replace(',', '.'):value;
        var num =  parseFloat(str);
        var roundedNum = num.toFixed(2);
        roundedNum = roundedNum.replace('.', ',');
        elem.value = roundedNum;
        return true;
    }

    note = elem.getAttribute('errorNote');
    displayErrorNote('',elem, note);
    return false;
}
function checkDecimalDotFormat(elem){
    removeErrorNote(elem);

    var value = elem.value;
    valid = false;
    if(value==""){
        return true;
    }

    valid = value.match(/^[0-9]*\,?[0-9]*$|^[0-9]*\.?[0-9]*$/);
    if(valid){
        var str= (value.indexOf('.')==-1)?value.replace(',', '.'):value;
        var num =  parseFloat(str);
        var roundedNum = num.toFixed(2);
        roundedNum = roundedNum.replace(',', '.');
        elem.value = roundedNum;
        return true;
    }
    return false;
}

